package com.zhiyou.crud.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.validation.Valid;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.validation.FieldError;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import com.zhiyou.crud.bean.Msg;
import com.zhiyou.crud.bean.user;
import com.zhiyou.crud.service.userService;

@Controller
public class userController {
@Autowired
userService userservice;
@RequestMapping("/getUser")
public  String  getUser(String usName,String usPassword,Map<String,Object> map) {
	   user   use=userservice.getUser(usName,usPassword);
	  if(use!=null) {
		  map.put("use", use);
		  return "index2";
	  }else {
		  return "redirect:index";
	  }
	   
}
@RequestMapping(value="/addUser")
public String addUser(user use) {		
			System.out.println(Msg.success().getCode());
		int i=userservice.addUser(use);
			if(i>0) {
		
		return	"redirect:login.jsp";
   }else {
			return	"redirect:register.jsp";
			}
	}   
@RequestMapping("/checkuser")
@ResponseBody
public Msg checkUser(@RequestParam("usname") String usname) {
	System.out.println(usname+"usname==========");
	boolean b=userservice.checkUser(usname);
	System.out.println("b==========="+b);
	if(b) {
		return Msg.success();
	}else {
		return Msg.fail();
	}
}
















}
